﻿using BLL;
using Newtonsoft.Json;
using Org.BouncyCastle.Tsp;
using Ozone.BLL;
using Ozone.Models;
using Ozone.Views.MethodCenter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Ozone.Views.SystemCenter.LiquidRatioMethod
{
    /// <summary>
    /// LiquidRatioMethodList.xaml 的交互逻辑
    /// </summary>
    public partial class LiquidRatioMethodList : UserControl
    {
        List<sys_dict> DictList = null;
        List<ObtainParam> ListSolutionBottles = new List<ObtainParam>();
        public LiquidRatioMethodList()
        {
            InitializeComponent();
            //默认加载第一条方法
            DictList = Sys_DictBLL.GetDictInfoList("");
            //刷新列表
            refreshMethodMain();
            //默认选择第一行
            if (dgMethodList.Items.Count > 0)
            {
                dgMethodList.SelectedIndex = 0;
            }
        }

        /// <summary>
        /// 刷新方法列表
        /// </summary>
        public void refreshMethodMain()
        {
            dgMethodList.ItemsSource = null;
            dgMethodList.ItemsSource = DictList;
        }

        #region 方法序列操作

        /// <summary>
        /// 执行方法操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonMethodMain_click(object sender, System.Windows.RoutedEventArgs e)
        {
            string tag = ((Button)sender).Tag.ToString();

            if (tag == "ADD")
            {
                WindowMethod form = new WindowMethod("新建配液方法", "");
                form.ShowDialog();
                if (form.isOK)
                {
                    string methodName = form.m_strName;
                    string Message = string.Empty;
                    sys_dict sys_DictModel = new sys_dict();
                    sys_DictModel.FID = Guid.NewGuid().ToString();
                    sys_DictModel.ItemCode = "";
                    sys_DictModel.ItemName = methodName;
                    sys_DictModel.ItemType = 1;
                    sys_DictModel.ExtendData = "";
                    sys_DictModel.IsEnabled = 1;
                    sys_DictModel.CreateTime = DateTime.Now;
                    sys_DictModel.ModifyTime = DateTime.Now;
                    bool IsSuccess = Sys_DictBLL.AddDictInfo(sys_DictModel);
                    if (!IsSuccess)
                    {
                        if (MessageBox.Show(Message, "提示",
                          MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
                        {
                            return;
                        }
                    }
                }
            }

            if (tag == "UPDATE")
            {
                if (dgMethodList.SelectedItems != null)
                {
                    sys_dict dict = (sys_dict)dgMethodList.SelectedItem;
                    if (dict != null)
                    {
                        WindowMethod form = new WindowMethod("修改配液方法", dict.ItemName);
                        form.ShowDialog();
                        if (form.isOK)
                        {
                            dict.ItemName = form.m_strName;
                            string Message = string.Empty;
                            bool IsSuccess = Sys_DictBLL.UpdateDictInfo(dict);
                            if (!IsSuccess)
                            {
                                if (MessageBox.Show(Message, "提示",
                                  MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
                                {
                                    return;
                                }
                            }
                        }
                    }

                }
            }

            if (tag == "DEL")
            {
                if (dgMethodList.SelectedItems != null)
                {
                    sys_dict dict = (sys_dict)dgMethodList.SelectedItem;
                    if (dict != null)
                    {
                        if (MessageBox.Show("确认要删除配液方法吗", "提示",
        MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
                        {
                            return;
                        }
                        Sys_DictBLL.DeleteDictInfo(dict.FID);
                    }
                }
                if (dgMethodList.SelectedItems.Count == 0)
                {
                    return;
                }
            }

            DictList = Sys_DictBLL.GetDictInfoList("");
            refreshMethodMain();
        }


        /// <summary>
        /// 执行方法序列操作 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonMethodItem_click(object sender, RoutedEventArgs e)
        {

            string tag = ((Button)sender).Tag.ToString();
            if (dgMethodList.SelectedItems != null)
            {
                sys_dict method = (sys_dict)dgMethodList.SelectedItem;
                if (method != null && !string.IsNullOrEmpty(method.ExtendData))
                {
                    ListSolutionBottles = JsonConvert.DeserializeObject<List<ObtainParam>>(method.ExtendData);
                }
                else
                {
                    ListSolutionBottles.Clear();
                }

                if (tag == "ADD")
                {
                    ObtainParam itemMaterial = new ObtainParam();
                    LiquidRatioMethodItemBottle form = new LiquidRatioMethodItemBottle(itemMaterial);
                    if (form.ShowDialog() == true)
                    {

                        //判断该配液瓶号是否已经配置过
                        bool exists = ListSolutionBottles.Exists(x => x.FBottleNo == form.obtainParam.FBottleNo);
                        if (exists)
                        {
                            if (MessageBox.Show("该溶液已在方法中配置", "提示",
                            MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
                            {

                            }
                            return;
                        }
                        else
                        {
                            ListSolutionBottles.Add(form.obtainParam);
                            dgMethodItem.ItemsSource = null;
                            dgMethodItem.ItemsSource = ListSolutionBottles;
                            method.ExtendData = JsonConvert.SerializeObject(ListSolutionBottles);
                            bool IsSuccess = Sys_DictBLL.UpdateDictInfo(method);
                        }
                    }
                }

                if (tag == "UPDATE")
                {

                    if (dgMethodItem.SelectedItem == null)
                    {
                        if (MessageBox.Show("请选择一行数据进行修改", "提示",
                      MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
                        {

                        }
                        return;
                    }
                    int nIndex = dgMethodItem.Items.IndexOf(dgMethodItem.SelectedItem);

                    ObtainParam itemMaterial = ListSolutionBottles[nIndex];


                    LiquidRatioMethodItemBottle form = new LiquidRatioMethodItemBottle(itemMaterial);
                    if (form.ShowDialog() == true)
                    {
                        dgMethodItem.ItemsSource = null;
                        dgMethodItem.ItemsSource = ListSolutionBottles;
                        method.ExtendData = JsonConvert.SerializeObject(ListSolutionBottles);
                        bool IsSuccess = Sys_DictBLL.UpdateDictInfo(method);
                    }
                }

                if (tag == "DEL")
                {

                    if (dgMethodItem.SelectedItem == null)
                    {
                        if (MessageBox.Show("请选择一行数据进行删除", "提示",
                      MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
                        {

                        }
                        return;
                    }
                    int nIndex = dgMethodItem.Items.IndexOf(dgMethodItem.SelectedItem);

                    if (MessageBox.Show("确认要删除吗", "提示",
                      MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
                        return;

                    ListSolutionBottles.RemoveAt(nIndex);
                    dgMethodItem.ItemsSource = null;
                    dgMethodItem.ItemsSource = ListSolutionBottles;
                    method.ExtendData = JsonConvert.SerializeObject(ListSolutionBottles);
                    bool IsSuccess = Sys_DictBLL.UpdateDictInfo(method);
                }

                refreshMethodItem(method);

            }
        }

        /// <summary>
        /// 选择方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgMethodMain_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
        {
            if (dgMethodList.SelectedItems.Count == 0)
            {
                return;
            }
            int nIndex = dgMethodList.Items.IndexOf(dgMethodList.SelectedItem);
            sys_dict method = DictList[nIndex];
            refreshMethodItem(method);
        }
        public void refreshMethodItem(sys_dict method)
        {
            dgMethodItem.ItemsSource = null;
            if (method != null && !string.IsNullOrEmpty(method.ExtendData))
            {
                List<ObtainParam> list = JsonConvert.DeserializeObject<List<ObtainParam>>(method.ExtendData);
                dgMethodItem.ItemsSource = list;
            }
        }

        #endregion

    }
}
